package com.cn.jinl.controller;


import com.cn.jinl.utils.WordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;

@Controller
@RequestMapping("dwfile")
public class FileOperateController {

    @ResponseBody
    @RequestMapping("/downloadFile")
    public void downloadFile(String path, HttpServletResponse response) throws Exception {
        String name = StringUtils.isNotBlank(path)?path:"";
        name = URLDecoder.decode(name,"utf-8");
        name = new String(name.getBytes("ISO-8859-1"),"utf-8");
        InputStream is = null;
        OutputStream os = null;
        try {
            File file = new File(name);
            // 清空response
            response.reset();
            // 设置response的Header
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getName(), "utf-8"));
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            is = new FileInputStream(file);
            os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            closeInputStream(is);
            closeOutputStream(os);
        }
    }

    @ResponseBody
    @RequestMapping("/downloadMaterialTempFile")
    public void downloadMaterialTempFile(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String path = request.getSession().getServletContext().getRealPath("/WEB-INF/classes/");
        String fileName = path + "material_template.xlsx";
        this.downloadFile(fileName,response);
    }

    @ResponseBody
    @RequestMapping("/downloadDeviceTempFile")
    public void downloadDeviceTempFile(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String path = request.getSession().getServletContext().getRealPath("/WEB-INF/classes/");
        String fileName = path + "device_item.xlsx";
        this.downloadFile(fileName,response);
    }


    private static void closeInputStream(InputStream is) {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void closeOutputStream(OutputStream os) {
        try {
            if (os != null) {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
